// Javascript file that makes ajax calls

function ajaxcall(){
	jQuery.post(
		MyAjax.ajaxurl,
		{
			action: 'list_gems',
			postID: MyAjax.postID,
			postCommentNonce: MyAjax.postCommentNonce
		},
		function (response){
			alert(response);
		},
		"json"
		);

}

function uploadGem(fileToUpload){
	// Build an FormData for upload
	var fd=new FormData();
	fd.append("name","Html 5 File API/FormData");
	fd.append("fileToUpload",fileToUpload);
	fd.append("action","uploadgem");
  //Reset upload bar
  document.getElementById('progressbar').innerHTML = 'unable to compute';
  document.getElementById('uploadbar').style.width="0%";
	var xhr = new XMLHttpRequest();
        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.addEventListener("load", uploadComplete, false);
        xhr.addEventListener("error", uploadFailed, false);
        xhr.addEventListener("abort", uploadCanceled, false);
        xhr.open("POST", MyAjax.ajaxurl);
        xhr.send(fd);
        function uploadProgress(evt) {
        if (evt.lengthComputable) {
          var percentComplete = Math.round(evt.loaded * 100 / evt.total);
          document.getElementById('progressbar').innerHTML = percentComplete.toString() + '%';
          document.getElementById('uploadbar').style.width=percentComplete.toString() + '%';
        }
        else {
          document.getElementById('progressbar').innerHTML = 'unable to compute';
          document.getElementById('uploadbar').style.width="0%";
        }
      }

      function uploadComplete(evt) {
        /* This event is raised when the server send back a response */
        alert(evt.target.responseText);
      }

      function uploadFailed(evt) {
        alert("There was an error attempting to upload the file.");
      }

      function uploadCanceled(evt) {
        alert("The upload has been canceled by the user or the browser dropped the connection.");
      }
}